package com.yeebo.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.yeebo.common.BaseController;
import com.yeebo.enums.admin.ResourceTypeEnums;
import com.yeebo.interceptor.Access;
import com.yeebo.rpc.admin.SysResourceService;
import com.yeebo.utils.ResponseUtils;
import com.yeebo.vo.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("sysResource/manage")
public class SysResourceManageController extends BaseController {

    @Resource
    private SysResourceService sysResourceService;

    @Access
    @RequestMapping(value = "/loadMenuList")
    @ResponseBody
    @RequiresPermissions("MENU_2019109103212149")
    public Result loadMenuList(@RequestBody JSONObject requestBody,
                               HttpServletRequest request) {
        String filterMenuName = requestBody.getString("filterMenuName");
        return ResponseUtils.success(sysResourceService.loadSysResourceManageList(ResourceTypeEnums.MENU.getType(), filterMenuName, 0L));
    }

    @Access
    @PostMapping(value = "/loadTopLevelMenuList")
    @ResponseBody
    public Result loadTopLevelMenuList(HttpServletRequest request) {
        return ResponseUtils.success(sysResourceService.loadTopLevelMenuList());
    }

    @Access
    @PostMapping(value = "/addMenu")
    @RequiresPermissions("RESOURCE_2019109103603606")
    @ResponseBody
    public Result addMenu(@RequestBody JSONObject requestBody,
                          HttpServletRequest request) {
        String resourceCode = requestBody.getString("resourceCode");
        String menuName = requestBody.getString("menuName");
        String menuDesc = requestBody.getString("menuDesc");
        Long parentMenuId = requestBody.getLongValue("parentMenuId");
        String menuIcon = requestBody.getString("menuIcon");
        String menuUrl = requestBody.getString("menuUrl");
        Integer sequence = requestBody.getInteger("sequence");
        sysResourceService.addMenu(resourceCode, menuName, menuDesc, parentMenuId, menuIcon, menuUrl, sequence);
        return ResponseUtils.success("成功添加菜单资源");
    }

    @Access
    @PostMapping(value = "/loadResourceList")
    @RequiresPermissions("MENU_2019109103212149")
    @ResponseBody
    public Result loadResourceList(@RequestBody JSONObject requestBody,
                                   HttpServletRequest request) {
        String filterResourceName = requestBody.getString("filterResourceName");
        Long parentMenuId = requestBody.getLongValue("parentMenuId");
        return ResponseUtils.success(sysResourceService.loadSysResourceManageList(ResourceTypeEnums.BUTTON.getType(), filterResourceName, parentMenuId));
    }

    @Access
    @PostMapping(value = "/addResource")
    @RequiresPermissions("RESOURCE_2019109103603606")
    @ResponseBody
    public Result addResource(@RequestBody JSONObject requestBody,
                              HttpServletRequest request) {
        String resourceCode = requestBody.getString("resourceCode");
        String menuName = requestBody.getString("menuName");
        String menuDesc = requestBody.getString("menuDesc");
        Long parentMenuId = requestBody.getLongValue("parentMenuId");
        Integer sequence = requestBody.getInteger("sequence");
        sysResourceService.addOperation(resourceCode, menuName, menuDesc, parentMenuId, "", "", sequence);
        return ResponseUtils.success("成功添加资源");
    }

    @Access
    @PostMapping(value = "/getResourceInfoById")
    @ResponseBody
    public Result getResourceInfoById(@RequestBody JSONObject requestBody,
                                      HttpServletRequest request) {
        Long resourceId = requestBody.getLongValue("resourceId");
        return ResponseUtils.success(sysResourceService.getResourceById(resourceId));
    }

    @Access
    @PostMapping(value = "/updateSysResource")
    @RequiresPermissions("RESOURCE_2019109103624284")
    @ResponseBody
    public Result updateSysResource(@RequestBody JSONObject requestBody,
                                    HttpServletRequest request) {
        Long resourceId = requestBody.getLongValue("resourceId");
        String resourceName = requestBody.getString("resourceName");
        String resourceDesc = requestBody.getString("resourceDesc");
        Long parentMenuId = requestBody.getLongValue("parentMenuId");
        String menuIcon = requestBody.getString("menuIcon");
        String menuUrl = requestBody.getString("menuUrl");
        Integer sequence = requestBody.getInteger("sequence");
        sysResourceService.updateSysResource(resourceId, resourceName, resourceDesc, parentMenuId, menuIcon, menuUrl, sequence);
        return ResponseUtils.success("更新成功");
    }

    @Access
    @PostMapping(value = "/deleteSysResource")
    @RequiresPermissions("RESOURCE_2019109103624284")
    @ResponseBody
    public Result deleteSysResource(@RequestBody JSONObject requestBody,
                                    HttpServletRequest request) {
        Long resourceId = requestBody.getLongValue("resourceId");
        sysResourceService.deleteSysResourceById(resourceId);
        return ResponseUtils.success("删除成功");
    }

}
